package cn.ctodb.cms.service;

import cn.ctodb.cms.domain.CmsContent;
import cn.ctodb.cms.repository.CmsContentRepository;
import cn.ctodb.cms.repository.search.CmsContentSearchRepository;
import cn.ctodb.cms.service.dto.CmsContentDTO;
import cn.ctodb.cms.service.mapper.CmsContentMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing CmsContent.
 */
@Service
@Transactional
public class CmsContentService {

    private final Logger log = LoggerFactory.getLogger(CmsContentService.class);
    
    @Inject
    private CmsContentRepository cmsContentRepository;

    @Inject
    private CmsContentMapper cmsContentMapper;

    @Inject
    private CmsContentSearchRepository cmsContentSearchRepository;

    /**
     * Save a cmsContent.
     *
     * @param cmsContentDTO the entity to save
     * @return the persisted entity
     */
    public CmsContentDTO save(CmsContentDTO cmsContentDTO) {
        log.debug("Request to save CmsContent : {}", cmsContentDTO);
        CmsContent cmsContent = cmsContentMapper.cmsContentDTOToCmsContent(cmsContentDTO);
        cmsContent = cmsContentRepository.save(cmsContent);
        CmsContentDTO result = cmsContentMapper.cmsContentToCmsContentDTO(cmsContent);
        cmsContentSearchRepository.save(cmsContent);
        return result;
    }

    /**
     *  Get all the cmsContents.
     *  
     *  @return the list of entities
     */
    @Transactional(readOnly = true) 
    public List<CmsContentDTO> findAll() {
        log.debug("Request to get all CmsContents");
        List<CmsContentDTO> result = cmsContentRepository.findAllWithEagerRelationships().stream()
            .map(cmsContentMapper::cmsContentToCmsContentDTO)
            .collect(Collectors.toCollection(LinkedList::new));

        return result;
    }

    /**
     *  Get one cmsContent by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Transactional(readOnly = true) 
    public CmsContentDTO findOne(Long id) {
        log.debug("Request to get CmsContent : {}", id);
        CmsContent cmsContent = cmsContentRepository.findOneWithEagerRelationships(id);
        CmsContentDTO cmsContentDTO = cmsContentMapper.cmsContentToCmsContentDTO(cmsContent);
        return cmsContentDTO;
    }

    /**
     *  Delete the  cmsContent by id.
     *
     *  @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete CmsContent : {}", id);
        cmsContentRepository.delete(id);
        cmsContentSearchRepository.delete(id);
    }

    /**
     * Search for the cmsContent corresponding to the query.
     *
     *  @param query the query of the search
     *  @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<CmsContentDTO> search(String query) {
        log.debug("Request to search CmsContents for query {}", query);
        return StreamSupport
            .stream(cmsContentSearchRepository.search(queryStringQuery(query)).spliterator(), false)
            .map(cmsContentMapper::cmsContentToCmsContentDTO)
            .collect(Collectors.toList());
    }
}
